# _*_ coding: utf-8 _*_
import queue
from PyQt5 import QtCore
from .counter import Counter
from .NovelThread import NovelThread

class QNovelThread(QtCore.QThread):
    # 数据队列集
    dataQueue = queue.Queue()

    # 计数器对象
    counter = Counter()

    # 状态栏信号对象
    statusSignal = QtCore.pyqtSignal(str)

    # 进度条信号对象
    progressSignal = QtCore.pyqtSignal(int)

    # 按钮状态信号对象
    btnStateSignal = QtCore.pyqtSignal()

    # 线程列表
    threads = None
    def __init__(self, toDir: str, threadNum: int):
        super(QNovelThread, self).__init__()
        self.toDir = toDir
        self.threadNum = threadNum


    def run(self) -> None:
        # 开始任务线程
        self.threads = []
        NovelThread.endFlag = True
        for _ in range(self.threadNum):
            thread = NovelThread(
                dataQueue=self.dataQueue,
                counter=self.counter,
                statusSignal=self.statusSignal,
                progressSignal=self.progressSignal,
                toDir=self.toDir
            )
            thread.start()
            self.threads.append(thread)

        # 等待队列集数据处理完成
        self.dataQueue.join()
        # 结束线程
        self.threads[0].stop()
        # 传输任务结束信号
        self.statusSignal.emit("Task Done")

        # 结束按钮不可点击状态
        self.btnStateSignal.emit()

